import configparser
import os
import socket
import platform

class Config:
    conf = ""

    def __init__(self,basepath=""):
        sys_platform = platform.platform().lower()
        print(sys_platform)
        if basepath == "":
            if sys_platform.find("windows") >= 0:
                cfgpath = "d:\\intellect\\config.ini"
            else:
                cfgpath = "/root/python/code/config/config.ini"

            # 创建管理对象
            self.conf = configparser.ConfigParser()
            # 读ini文件
            self.conf.read(cfgpath, encoding="utf-8")  # python3

        else:
            if sys_platform.find("windows") >= 0:
                cfgpath = basepath + "\\application.ini"
            else:
                cfgpath = basepath + "/application.ini"

            # 创建管理对象
            self.conf = configparser.ConfigParser()
            # 读ini文件
            self.conf.read(cfgpath, encoding="utf-8")  # python3
            env = self.conf.get("application", "active")
            self.env = env
            if sys_platform.find("windows") >= 0:
                cfgpath = basepath + "\\application-" + env + ".ini"
            else:
                cfgpath = basepath + "/application-" + env + ".ini"
            self.conf = configparser.ConfigParser()
            # 读ini文件
            self.conf.read(cfgpath, encoding="utf-8")  # python3

    def getApi(self):
        print(self.conf.sections())
        return {
            "base": self.conf.get("api","base"),
            "host": self.conf.get("api", "host"),
        }

    def getDB(self):
        return [self.conf.get("stock","host"),
                self.conf.get("stock","user"),
                self.conf.get("stock","pass"),
                self.conf.getint("stock","port"),
                self.conf.get("stock","dbname")
                ]

    def getRedis(self):
        return [self.conf.get("redis","host"),
                self.conf.get("redis","password"),
                self.conf.getint("redis","port"),
                self.conf.getint("redis","db")
                ]

    def getStockData(self):
        return {"datadir":self.conf.get("stock","datadir"),
                "mergedir":self.conf.get("stock","mergedir")
                }